import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SignThread extends Thread {

	private boolean isOver = false;
	private boolean isTest = false;
	public boolean isOver() {
		return isOver;
	}

	public void setOver(boolean isOver) {
		this.isOver = isOver;
	}

	@Override
	public void run() {
		super.run();
		String currentdate = "";
		String employeeId = "00000185";
		Calendar now = Calendar.getInstance();
		int hour;
		while (!isOver()) {
			now = Calendar.getInstance();
			if(isTest){
				System.out.println("年: " + now.get(Calendar.YEAR));
				System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
				System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
				System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
				System.out.println("分: " + now.get(Calendar.MINUTE));
				System.out.println("秒: " + now.get(Calendar.SECOND));
				System.out.println("当前时间毫秒数：" + now.getTimeInMillis());
				System.out.println(now.getTime());
			}
			hour = now.get(Calendar.HOUR_OF_DAY);
//			Scanner in = new Scanner(System.in);
//			hour = in.nextInt();
			
			if(hour == 0 ){
				Date nowdate = new Date();
				// format date pattern
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				currentdate = formatter.format(nowdate);

				Map<String, String> params = new HashMap<String, String>();
				params.put("currentdate", currentdate);
				params.put("employeeId", employeeId);
				params.put("inOutFlag", "0");

				String signIn = null;
				String signOut = null;
				String signInparamStr = EncryptUtils.getParameterStr(params);
				try {
					signIn = EncryptUtils.encode(signInparamStr);
					if(isTest){
						System.out.println("签到字符串:" + signIn);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}

				Map<String, String> signOutparams = new HashMap<String, String>();
				signOutparams.put("currentdate", currentdate);
				signOutparams.put("employeeId", employeeId);
				signOutparams.put("inOutFlag", "1");

				String signOutParams = EncryptUtils.getParameterStr(signOutparams);
				try {
					signOut = EncryptUtils.encode(signOutParams);
					if(isTest){
						System.out.println("签退字符串:" + signOut);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				if(WriteStringToTxt.WriteStringToFile("./signin.txt", signIn)){
					System.out.println("写入签到加密字符串文件成功");
				}
				if(WriteStringToTxt.WriteStringToFile("./signout.txt", signOut)){
					System.out.println("写入签退加密字符串文件成功");
					System.out.print(">>");
				}
				
//				try {
//					Thread.sleep(1000*30);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//					isOver = true;
//				}
				
				if(hour >=23 || hour < 1)
				{
					try {
						Thread.sleep(1000*60*20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						isOver = true;
					}
				}
				else {
					try {
						Thread.sleep(1000*60*50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						isOver = true;
					}
				}
			}
		}
		System.out.println("自动产生签到，签退，加密串退出运行");
	}
	
	

}
